package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcAnnouncement;
import com.ruoyi.fun.service.IEcAnnouncementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公告Controller
 * 
 * @author ruoyi
 * @date 2025-08-08
 */
@RestController
@RequestMapping("/fun/announcement")
public class EcAnnouncementController extends BaseController
{
    @Autowired
    private IEcAnnouncementService ecAnnouncementService;

    /**
     * 查询公告列表
     */
    @PreAuthorize("@ss.hasPermi('fun:announcement:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcAnnouncement ecAnnouncement)
    {
        startPage();
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecAnnouncement.setOrgId(orgId);
        List<EcAnnouncement> list = ecAnnouncementService.selectEcAnnouncementList(ecAnnouncement);
        return getDataTable(list);
    }

    /**
     * 导出公告列表
     */
    @PreAuthorize("@ss.hasPermi('fun:announcement:export')")
    @Log(title = "公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcAnnouncement ecAnnouncement)
    {
        List<EcAnnouncement> list = ecAnnouncementService.selectEcAnnouncementList(ecAnnouncement);
        ExcelUtil<EcAnnouncement> util = new ExcelUtil<EcAnnouncement>(EcAnnouncement.class);
        util.exportExcel(response, list, "公告数据");
    }

    /**
     * 获取公告详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:announcement:query')")
    @GetMapping(value = "/{announcementId}")
    public AjaxResult getInfo(@PathVariable("announcementId") Long announcementId)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        return success(ecAnnouncementService.selectEcAnnouncementByAnnouncementIdAndOrgId(announcementId, orgId));
    }

    /**
     * 新增公告
     */
    @PreAuthorize("@ss.hasPermi('fun:announcement:add')")
    @Log(title = "公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcAnnouncement ecAnnouncement)
    {
        ecAnnouncement.setOrgId(getOrgId());
        return toAjax(ecAnnouncementService.insertEcAnnouncement(ecAnnouncement));
    }

    /**
     * 修改公告
     */
    @PreAuthorize("@ss.hasPermi('fun:announcement:edit')")
    @Log(title = "公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcAnnouncement ecAnnouncement)
    {
        return toAjax(ecAnnouncementService.updateEcAnnouncement(ecAnnouncement));
    }

    /**
     * 删除公告
     */
    @PreAuthorize("@ss.hasPermi('fun:announcement:remove')")
    @Log(title = "公告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{announcementIds}")
    public AjaxResult remove(@PathVariable Long[] announcementIds)
    {
        return toAjax(ecAnnouncementService.deleteEcAnnouncementByAnnouncementIds(announcementIds));
    }
    /**
     * 根据announcementId和组织ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:ecannouncement:query')")
    @GetMapping(value = "/{announcementId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("announcementId") Long announcementId, @PathVariable("orgId") Long orgId)
    {
        return success(ecAnnouncementService.selectEcAnnouncementByAnnouncementIdAndOrgId(announcementId, orgId));
    }

    /**
     * 根据组织ID查询列表
     */
    @PreAuthorize("@ss.hasPermi('fun:ecannouncement:list')")
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        EcAnnouncement ecAnnouncement = new EcAnnouncement();
        List<EcAnnouncement> list = ecAnnouncementService.selectEcAnnouncementListByOrgId(ecAnnouncement, orgId);
        return getDataTable(list);
    }


}